<?php

class Tools_SuperUpdate_Discuz {

    public $arrConfig;
    public $init = false;

    public function setConfig($arrConfig) {
        $this->arrConfig = $arrConfig;
        $this->init = true;
    }

    public function spHtmlText($str) {
        $str = strip_tags($str, '<br> <p> <hr>');
        $str = str_replace('<br>', PHP_EOL, $str);
        $str = str_replace('<p>', PHP_EOL, $str);
        $str = str_replace('<br />', PHP_EOL, $str);
        $str = str_replace('</p>', PHP_EOL, $str);
        $str = str_replace('<hr>', '[hr]', $str);
        $str = str_replace('&nbsp;', ' ', $str);
        $str = str_replace('  ', ' ', $str);
        $str = trim($str);
        return $str;
    }

    /**
     * 设置list表的名字
     */
    public function setListTableName($listTableName) {
        $this->listTableName = $listTableName;
    }

    /**
     * 设置content表的名字
     */
    public function setContentTableName($contentTableName) {
        $this->contentTableName = $contentTableName;
    }

    /**
     * 设置本地ID在list表的名字
     */
    public function setListTabelLocalIdFieldName($listTabelLocalIdFieldName) {
        $this->listTabelLocalIdFieldName = $listTabelLocalIdFieldName;
    }

    /**
     * 设置本地ID在content表的名字
     */
    public function setContentTableLocalIdFieldName($contentTableLocalIdFieldName) {
        $this->contentTableLocalIdFieldName = $contentTableLocalIdFieldName;
    }

    /**
     * 设置主题每次发多少
     * @param type $listSelectLimit
     */
    public function setListSelectLimit($listSelectLimit) {
        $this->listSelectLimit = $listSelectLimit;
    }

    /**
     * 设置帖子每次取多少
     * @param type $contentSelectLimit
     */
    public function setContentSelectLimit($contentSelectLimit) {
        $this->contentSelectLimit = $contentSelectLimit;
    }

    public function setContentBodyFieldName($contentBodyFieldName) {
        $this->contentBodyFieldName = $contentBodyFieldName;
    }

    public function run() {
        $currUserPub15sLimit = NULL;
        $discuz_post = new discuz_post();
        foreach ($this->arrConfig as $strFenqu => $arrBankuai) {
            foreach ($arrBankuai as $strBankuai => $arrSqlLike) {
                $fid = getServerForumId($strFenqu, $strBankuai);
                if (!$fid) {
                    $fid = getServerForumId($strFenqu, $strBankuai, false);
                }
                if (!$fid) {
                    log::error('ERROR:Can NOT Get The Right $fid!');
                    log::dw($strFenqu . '|' . $strBankuai);
                    exit;
                }

                $listtable = $this->listTableName;
                $limit = mt_rand(1, $this->listSelectLimit);

                $sqllike = '(title';

                foreach ($arrSqlLike as $strSqlLike) {
                    $sqllike.=" like '%{$strSqlLike}%' or title";
                }

                if (!$sqllike) {
                    log::error('ERROR:$sqllike:' . $sqllike);
                    log::dw($strFenqu . '|' . $strBankuai);
                    exit;
                }

                $sqllike = substr($sqllike, 0, -strlen('or title')) . ')';

                $query = "SELECT * FROM `{$listtable}` WHERE {$sqllike} AND haspub=0 ORDER BY rand() LIMIT $limit";
                log::n($query);
                $threadlist = db_select($query);
                log::n('count($threadlist):' . count($threadlist));

                foreach ($threadlist as $thread) {
                    $user = getDiscuzRandPubUser();
                    if (mt_rand(1, 100) > 70 || !$user || $user == $currUserPub15sLimit) {
                        registerDiscuzUserByHttp();
                    }
                    $listTableLocalIdKey = $this->listTabelLocalIdFieldName;
                    $localThreadId = $thread[$listTableLocalIdKey];
                    $title = $thread['title'];

                    $contenttable = $this->contentTableName;
                    $limit = mt_rand(1, $this->listSelectLimit);
                    $contentTableLocalIdKey = $this->contentTableLocalIdFieldName;

                    $query = "SELECT * FROM $contenttable WHERE `$contentTableLocalIdKey`='$localThreadId' And tType='thread' LIMIT 1";
                    log::n($query);
                    $threadcontent = db_select($query);
                    if (!$title) {
                        $title = $threadcontent[0]['title'];
                    }
                    $body = $threadcontent[0][$this->contentBodyFieldName];
                    if (!$body) {
                        $body = $title;
                    }
                    $title = strip_tags($title);
                    if (strlen($body) < 30) {
                        $body = $title . PHP_EOL . $body;
                    }

                    if (!$user || $user == $currUserPub15sLimit) {
                        $user = getDiscuzRandPubUser();
                    }
                    $discuz_post->setCurrUser($user);
                    $currUserPub15sLimit = $user;
                    $title = $this->spHtmlText($title);
                    $body = $this->spHtmlText($body);
                    $tid = $discuz_post->fastPubThread($title, $body, $fid);
                    if ($tid) {
                        $query = "UPDATE {$listtable} SET haspub='$tid',`fid`='$fid' WHERE `{$listTableLocalIdKey}`=$localThreadId LIMIT 1";
                        log::n($query);
                        db_query($query);
                        sleep(10);
                    }
                }
                //搜索回帖并自动发布
                $limit = mt_rand(1, $this->contentSelectLimit);
                $query = "SELECT * FROM {$contenttable} as C,{$listtable} as T WHERE T.{$listTableLocalIdKey}=C.{$contentTableLocalIdKey} AND C.tType='post' AND C.haspub=0 AND T.haspub!=0  ORDER BY rand() LIMIT $limit";
                log::n($query);
                $postlist = db_select($query);
                log::n('count($postlist):' . count($postlist));
                foreach ($postlist as $postdata) {
                    $postId = $postdata['Id'];
                    $fid = $postdata['fid'];
                    $tid = $postdata['haspub'];
                    if (!$fid || !$tid) {
                        log::dw('$fid OR $tid is EMPTY! $fid:' . $fid . '|$tid:' . $tid);
                        continue;
                    }
                    if (mt_rand(1, 100) > 90) {
                        registerDiscuzUserByHttp();
                    }
                    $user = getDiscuzRandPubUser();
                    if (!$user || $user == $currUserPub15sLimit) {
                        $user = getDiscuzRandPubUser();
                        sleep(16);
                    }
                    $title = '';
                    $body = $postdata[$this->contentBodyFieldName];
                    $discuz_post->setCurrUser($user);
                    $currUserPub15sLimit = $user;
                    $title = $this->spHtmlText($title);
                    $body = $this->spHtmlText($body);
                    $discuz_post->fastPubPost($title, $body, $fid, $tid);
                    $query = "UPDATE $contenttable SET haspub=1 WHERE Id='{$postId}' LIMIT 1";
                    log::n($query);
                    db_query($query);
                    sleep(10);
                }
            }
        }
    }

}

?>